<?php
namespace app\index\model;

use think\Model;

class Order extends Model
{
    protected $table = 'orders';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';

    // *** 新增的类型转换配置 ***
    protected $type = [
        'create_time' => 'datetime',
        'update_time' => 'datetime',
        'pay_time'    => 'datetime', // 也为 pay_time 添加，以防万一
    ];
    // *************************

    public function orderItems()
    {
        return $this->hasMany('OrderItem');
    }

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function getOrderStatusTextAttr($value, $data)
    {
        $status = [0 => '待支付', 1 => '已支付', 2 => '已取消'];
        return $status[$data['order_status']] ?? '未知状态';
    }
}